<template>
    <div class="box-detail-page">
        <PageHeader 
            title="业务领域" 
            subtitle="易凡科技专业解决方案"
            :breadcrumbItems="[
                { text: '首页', path: '/' },
                { text: '业务领域', path: '/boxs' }
            ]"
            :currentText="currentNews.title || '业务详情'"
        />
        
        <div class="page-container">
            <el-row :gutter="30">
                <!-- 主内容区 -->
                <el-col :xs="24" :sm="24" :md="16" :lg="17" class="main-content">
                    <el-skeleton :loading="loading" animated>
                        <template #template>
                            <div class="skeleton-content">
                                <el-skeleton-item variant="h1" style="width: 50%;" />
                                <el-skeleton-item variant="text" style="margin: 10px 0; width: 30%;" />
                                <el-skeleton-item variant="p" style="margin: 10px 0" />
                                <el-skeleton-item variant="p" style="margin: 10px 0" />
                                <el-skeleton-item variant="p" style="margin: 10px 0" />
                            </div>
                        </template>
                        
                        <template #default>
                            <div class="detail-content">
                                <h1 class="detail-title">{{ currentNews.title }}</h1>
                                <div class="detail-meta">
                                    <span class="time">
                                        <i class="el-icon-calendar"></i> {{ whichTime(currentNews.editTime) }}
                                    </span>
                                </div>
                                
                                <el-divider>
                                    <el-icon><star-filled /></el-icon>
                                </el-divider>
                                
                                <div class="detail-content-html" v-html="currentNews.content"></div>
                                
                                <!-- 底部分享 -->
                                <div class="detail-footer">
                                    <h3>分享至:</h3>
                                    <div class="share-links">
                                        <a href="#" class="share-link"><i class="el-icon-s-promotion"></i></a>
                                        <a href="#" class="share-link"><i class="el-icon-s-custom"></i></a>
                                        <a href="#" class="share-link"><i class="el-icon-picture"></i></a>
                                    </div>
                                </div>
                            </div>
                        </template>
                    </el-skeleton>
                </el-col>
                
                <!-- 侧边栏 -->
                <el-col :xs="24" :sm="24" :md="8" :lg="7" class="sidebar">
                    <el-card class="recent-box-card" shadow="hover">
                        <template #header>
                            <div class="card-header">
                                <span class="card-title">相关业务</span>
                            </div>
                        </template>
                        
                        <el-skeleton :loading="loading" animated :count="4">
                            <template #template>
                                <div class="skeleton-item" v-for="i in 4" :key="i">
                                    <el-skeleton-item variant="p" style="width: 80%" />
                                    <el-skeleton-item variant="text" style="width: 40%" />
                                </div>
                            </template>
                            
                            <template #default>
                                <div 
                                    v-for="item in topNews" 
                                    :key="item._id" 
                                    class="recent-item"
                                    @click="handleChange(item._id)"
                                >
                                    <div class="item-title">{{ item.title }}</div>
                                    <div class="item-meta">
                                        <time class="time">
                                            <i class="el-icon-time"></i> {{ whichTime(item.editTime) }}
                                        </time>
                                    </div>
                                </div>
                            </template>
                        </el-skeleton>
                    </el-card>
                    
                    <!-- 联系我们卡片 -->
                    <el-card class="contact-card" shadow="hover">
                        <template #header>
                            <div class="card-header">
                                <span class="card-title">联系我们</span>
                            </div>
                        </template>
                        
                        <div class="contact-info">
                            <div class="contact-item">
                                <i class="el-icon-phone"></i>
                                <span>087-32544554-322</span>
                            </div>
                            <div class="contact-item">
                                <i class="el-icon-message"></i>
                                <span>contact@yifantech.com</span>
                            </div>
                            <div class="contact-item">
                                <i class="el-icon-location"></i>
                                <span>甘肃省兰州市皋兰县海河街6号</span>
                            </div>
                        </div>
                        
                        <el-button type="primary" class="contact-btn" @click="goToContact">
                            立即咨询
                        </el-button>
                    </el-card>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref, watchEffect, onBeforeUnmount } from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';
import moment from 'moment';
import { StarFilled } from '@element-plus/icons-vue';
import PageHeader from '@/components/PageHeader.vue';

moment.locale("zh-cn");
const route = useRoute();
const router = useRouter();
const currentNews = ref({});
const topNews = ref([]);
const loading = ref(true);

const stop = watchEffect(async () => {
    if(!route.params.id) return;
    loading.value = true;
    
    try {
        const [res1, res2] = await Promise.all([
            axios.get(`/webapi/boxs/list/${route.params.id}`),
            axios.get(`/webapi/boxs/toplist?limit=4`)
        ]);
        
        currentNews.value = res1.data.data[0];
        topNews.value = res2.data.data;
    } catch (error) {
        console.error('获取数据失败:', error);
    } finally {
        loading.value = false;
    }
});

onBeforeUnmount(() => {
    stop();
});

const whichTime = time => {
    return moment(time).format("lll");
};

// 跳转事件
const handleChange = (id) => {
    router.push(`/boxs/${id}`);
};

// 跳转到联系页面
const goToContact = () => {
    router.push('/touch');
};
</script>

<style lang="scss" scoped>
.box-detail-page {
    padding-bottom: 60px;
    
    .page-container {
        max-width: 1200px;
        padding: 0 15px;
        margin: 0 auto;
    }
    
    .main-content {
        margin-bottom: 30px;
        
        .detail-content {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            
            .detail-title {
                font-size: 28px;
                font-weight: 700;
                color: #333;
                margin-top: 0;
                margin-bottom: 15px;
            }
            
            .detail-meta {
                display: flex;
                align-items: center;
                margin-bottom: 20px;
                color: #909399;
                font-size: 14px;
                
                .time {
                    display: flex;
                    align-items: center;
                    
                    i {
                        margin-right: 5px;
                    }
                }
            }
            
            .detail-content-html {
                line-height: 1.8;
                color: #333;
                font-size: 16px;
                
                :deep(img) {
                    max-width: 100%;
                    height: auto;
                    border-radius: 4px;
                    margin: 20px 0;
                }
                
                :deep(p) {
                    margin-bottom: 16px;
                }
                
                :deep(h2), :deep(h3), :deep(h4) {
                    margin-top: 30px;
                    margin-bottom: 15px;
                    font-weight: 600;
                    color: #333;
                }
                
                :deep(ul), :deep(ol) {
                    padding-left: 20px;
                    margin-bottom: 16px;
                    
                    li {
                        margin-bottom: 8px;
                    }
                }
                
                :deep(blockquote) {
                    padding: 0 1em;
                    color: #6a737d;
                    border-left: 4px solid #eaecef;
                    margin: 0 0 16px;
                }
            }
            
            .detail-footer {
                margin-top: 40px;
                padding-top: 20px;
                border-top: 1px solid #eee;
                display: flex;
                align-items: center;
                
                h3 {
                    font-size: 16px;
                    font-weight: 600;
                    color: #333;
                    margin: 0 15px 0 0;
                }
                
                .share-links {
                    display: flex;
                    gap: 10px;
                    
                    .share-link {
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        width: 36px;
                        height: 36px;
                        border-radius: 50%;
                        background-color: #f5f7fa;
                        color: #606266;
                        text-decoration: none;
                        transition: all 0.3s ease;
                        
                        &:hover {
                            background-color: #409EFF;
                            color: #fff;
                        }
                        
                        i {
                            font-size: 18px;
                        }
                    }
                }
            }
        }
        
        .skeleton-content {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        }
    }
    
    .sidebar {
        .recent-box-card, .contact-card {
            margin-bottom: 20px;
            
            .card-header {
                padding: 0;
                
                .card-title {
                    font-size: 18px;
                    font-weight: 600;
                    color: #333;
                }
            }
            
            .recent-item {
                padding: 15px;
                border-bottom: 1px solid #f0f0f0;
                cursor: pointer;
                transition: all 0.3s;
                
                &:last-child {
                    border-bottom: none;
                }
                
                &:hover {
                    background-color: #f5f7fa;
                    
                    .item-title {
                        color: #409EFF;
                    }
                }
                
                .item-title {
                    font-size: 14px;
                    font-weight: 500;
                    color: #333;
                    margin-bottom: 8px;
                    transition: color 0.3s;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    overflow: hidden;
                }
                
                .item-meta {
                    .time {
                        font-size: 12px;
                        color: #909399;
                        display: flex;
                        align-items: center;
                        
                        i {
                            margin-right: 5px;
                            font-size: 14px;
                        }
                    }
                }
            }
            
            .skeleton-item {
                padding: 15px;
                border-bottom: 1px solid #f0f0f0;
                
                &:last-child {
                    border-bottom: none;
                }
            }
            
            .contact-info {
                margin-bottom: 20px;
                
                .contact-item {
                    display: flex;
                    align-items: center;
                    margin-bottom: 10px;
                    
                    i {
                        font-size: 16px;
                        color: #409EFF;
                        margin-right: 8px;
                    }
                    
                    span {
                        font-size: 14px;
                        color: #606266;
                    }
                }
            }
            
            .contact-btn {
                width: 100%;
            }
        }
    }
}
</style>